package com.gmail.avereshchaga.web.transport;

import com.gmail.avereshchaga.db.entity.IInvoiceItems;
import com.gmail.avereshchaga.db.entity.InvoiceItemsImpl;
import com.gmail.avereshchaga.db.service.IInvoiceItemsService;
import com.gmail.avereshchaga.db.service.IUserSessionIdService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Class performs the functions of the controller operations entity InvoiceItems
 *
 * @author <a href="mailto:avereshchaga@gmail.com">Vereshchaga Alexey</a>
 */
@Controller
public class InvoiceItemsTransport {

    @Autowired
    private IInvoiceItemsService invoiceItemsService;
    @Autowired
    private IUserSessionIdService userSessionIdService;

    /**
     * Call method of search list InvoiceItems by invoice id
     *
     * @param sessionId id defines the ability to perform the method
     * @param invoiceIdStr id invoice
     * @return list of InvoiceItems
     */
    @RequestMapping(value = "invoiceItemsByInvoiceId")
    @ResponseBody
    public List findInvoiceItemsByInvoiceId(@RequestParam("sessionId") String sessionId,
            @RequestParam("invoiceId") String invoiceIdStr) {
        List invoiceItems = new ArrayList();
        if (userSessionIdService.findBySessionId(sessionId)) {
            Long invoiceId = Long.parseLong(invoiceIdStr);
            invoiceItems = invoiceItemsService.findByInvoiceId(invoiceId);
        }
        return invoiceItems;
    }

    /**
     * Call method to save list InvoiceItems
     *
     * @param sessionId id defines the ability to perform the method
     * @param invoiceItems list of InvoiceItems to save
     * @return list of saved InvoiceItems
     * @throws Exception
     */
    @RequestMapping(value = "saveInvoiceItems")
    @ResponseBody
    public List saveInvoiceItems(@RequestParam("sessionId") String sessionId,
            @RequestParam("invoiceItems") String invoiceItems) throws Exception {
        List<IInvoiceItems> invoiceItemsListUpdated = new ArrayList<IInvoiceItems>();
        if (userSessionIdService.findBySessionId(sessionId)) {
            List<IInvoiceItems> invoiceItemsList = new ArrayList<IInvoiceItems>();

            ObjectMapper mapper = new ObjectMapper();
            try {
                invoiceItemsList = mapper.readValue(invoiceItems, new TypeReference<ArrayList<InvoiceItemsImpl>>() {
                });
            } catch (JsonParseException ex) {
                Logger.getLogger(InvoiceItemsTransport.class.getName()).log(Level.SEVERE, null, ex);
            } catch (JsonMappingException ex) {
                Logger.getLogger(InvoiceItemsTransport.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(InvoiceItemsTransport.class.getName()).log(Level.SEVERE, null, ex);
            }
            for (IInvoiceItems invoiceItem : invoiceItemsList) {
                if (invoiceItem.getInvoice() == null) {
                    invoiceItemsService.deleteInvoiceItems(invoiceItem);
                } else if (invoiceItem.getId() == null) {
                    invoiceItemsListUpdated.add(invoiceItemsService.createInvoiceItems(invoiceItem));
                } else if (invoiceItem.getId() != null) {
                    invoiceItemsListUpdated.add(invoiceItemsService.updateInvoiceItems(invoiceItem));
                } else {
                    throw new Exception("Unknown entity:" + invoiceItem.toString());
                }
            }
            return invoiceItemsListUpdated;
        } else {
            return invoiceItemsListUpdated;
        }
    }
}
